<?php

namespace app\admin\controller;

use app\common\model\CategoryModel;
use app\common\model\ArticleModel;
use think\facade\Request;
use utils\Upload;
use utils\Ueditor;

class ArticleController extends AdminBase
{
    public function lists(ArticleModel $articles)
    {
        if ($this->request->isAjax()) {
            $keyword=input('post.key');
            $type=input('post.type');
            $pageSize = $this->getPageSize();
            $where = [];
            $keyword && $where[] = ['title', 'like', "%$keyword%"];
            $type && $where[] = ['cid', '=', "$type"];
            $data = $articles
                ->where($where)
                ->order('aid desc')
                ->paginate($pageSize);
            foreach ($data as $key=>$value){
                $cate = CategoryModel::where(['cate_id' => $value['cid']])->field('cate_name')->find();
                $data[$key]['cate_name'] =$cate['cate_name'];
                $data[$key]['utime'] = date('Y-m-d H:i',$value['utime']);
            }
            $total = $data->total();
            $list = $data->items();
            return $this->listJson($list, $total);
        } else {
            $cate = $this->articleCate();
            $type = self::combineCate($cate);
            $this->assign('type',$type);
            return view();
        }
    }

    public function add(ArticleModel $articles)
    {
        if (Request::isAjax()) {
            $data = Request::param();
            if($data['endtime']) $data['endtime'] = strtotime($data['endtime']);
            $user = $this->app->user;
            $data['auther'] = $user['uid'];
            //处理百度编辑器图片
            $data['content'] = $info = Ueditor::add($data['content']);
            $article = $articles->addArticle($data);
            if ($article) {
                return ['code'=>$article['code'], 'msg'=>$article['msg']];
            } else {
                return ['code'=>$article['code'], 'msg'=>$article['msg']];
            }
        } else {
            $cate = $this->articleCate();
            $type = self::combineCate($cate);
            $this->assign('type',$type);
            return $this->view->fetch();
        }
    }

    public function delete(ArticleModel $articles)
    {
        $id = input('aid');
        $article = $articles->where(['aid'=>$id])->field('image,content')->find();
        if($article['image'] != '') Upload::unlink($article['image']);
        if($article['content'] != '') Ueditor::del($article['content']);

        ArticleModel::destroy($id);
    }

    public function edit()
    {
        if (Request::isAjax()) {
            $data = Request::param();
            $aid = $data['aid'];
            if($data['endtime']) $data['endtime'] = strtotime($data['endtime']);

            $info = ArticleModel::field('content')->get($aid);
            $data['content'] = Ueditor::edit($data['content'],$info['content']);

            unset($data['aid']);
            $data['utime'] = time();
            $article = ArticleModel::update($data, ['aid' => $aid]);
            if ($article) {
                return ['code'=>0,'msg'=>'修改成功'];
            } else {
                return ['code'=>1,'msg'=>'修改失败'];
            }
        } else {
            $aid = input('aid');
            $data = ArticleModel::get($aid);
            if($data['endtime']) $data['endtime'] = date('Y-m-d', $data['endtime']);
            $this->assign('data', $data);

            $cate = $this->articleCate();
            $type = self::combineCate($cate);
            $this->assign('type',$type);
            return $this->view->fetch();
        }
    }

    private function articleCate()
    {
        $cate = CategoryModel::where('cate_name', '文章分类')->field('cate_id')->find();
        $cids = CategoryModel::where('cate_pid', $cate['cate_id'])->field('cate_id')->all();
        $ids = '';
        foreach ($cids as $key => $value) {
            $ids .= $value['cate_id'].',';
        }
        $ids = $ids . $cate['cate_id'];
        return CategoryModel::whereIn('cate_id', $ids)->select()->toArray();
    }
}